// DlgReport.cpp : implementation file
//

#include "stdafx.h"
#include "BBViewer.h"
#include "DlgReport.h"

#include "StatusMgr.h"
#include "../_inc_util/UtilString.h"

//////////////////////////////////////////////////////////////////////////
enum 
{
	PrintOut_title = 0,
	PrintOut_Date,
	PrintOut_Place,
	PrintOut_GSensor,
	PrintOut_Memo

};

// CDlgReport dialog

IMPLEMENT_DYNAMIC(CDlgReport, CDialog)

CDlgReport::CDlgReport(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgReport::IDD, pParent)
{
	m_pViewerSetup = NULL;
}

CDlgReport::~CDlgReport()
{
}

void CDlgReport::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_STATIC_PREVIEW, m_staticPreview);
}


BEGIN_MESSAGE_MAP(CDlgReport, CDialog)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_BTN_INSERT_MEMO, &CDlgReport::OnBnClickedBtnInsertMemo)
	ON_BN_CLICKED(IDC_BTN_PRINT, &CDlgReport::OnBnClickedBtnPrint)
	ON_BN_CLICKED(IDC_CANCEL, &CDlgReport::OnBnClickedCancel)
END_MESSAGE_MAP()


// CDlgReport message handlers

BOOL CDlgReport::OnInitDialog()
{
	CDialog::OnInitDialog();

	LoadText();

	memset(&m_PrintInfo, 0, sizeof(PRINTINFO));

	return TRUE;
}

void CDlgReport::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	RECT rcClient;
	m_staticPreview.GetWindowRect(&rcClient);
	ScreenToClient(&rcClient);

	CString str;
	UINT nOemVal;

	nOemVal = 0;		//temporary


	CUtilString us;

	wsprintf(m_PrintInfo.tzTitle, _T("%s"), m_sarrText.GetAt(PrintOut_title));
	wsprintf(m_PrintInfo.tzBottom, _T("%s"), us.GetDateTimeString(time(0)-m_Timezone.GetTimeZone()));

	wsprintf(m_PrintInfo.tzLine1, _T("1. %s (yyyy-mm-dd hh:MM:ss)"), m_sarrText.GetAt(PrintOut_Date));
	wsprintf(m_PrintInfo.tzLine1C, _T("%s"), us.GetDateTimeString(m_ReportItem.dwTime));


	wsprintf(m_PrintInfo.tzLine2, _T("2. %s"), m_sarrText.GetAt(PrintOut_Place));
	wsprintf(m_PrintInfo.tzLine2C1, _T("%s,  "), us.GetGpsLatitude(m_ReportItem.fNGps));
	wsprintf(m_PrintInfo.tzLine2C2, _T("%s"), us.GetGpsLongitude(m_ReportItem.fEGps));
	wsprintf(m_PrintInfo.tzLine2C3, _T("%s"), us.GetSpeed(m_ReportItem.fSpeed, m_ReportItem.nSpeedMode));

	wsprintf(m_PrintInfo.tzLine3, _T("3. %s"), m_sarrText.GetAt(PrintOut_GSensor));
	wsprintf(m_PrintInfo.tzLine3C, _T("%s"), us.GetGSensor(m_ReportItem.fXSensor, m_ReportItem.fYSensor, m_ReportItem.fZSensor));

	wsprintf(m_PrintInfo.tzLine4, _T("4. %s"), m_sarrText.GetAt(PrintOut_Memo));



	CReportPreview  preview;
	preview.GetPrintOutDC(&dc, &rcClient, &m_ReportItem, &m_PrintInfo, m_pViewerSetup);

}

void CDlgReport::SetReportItem(PREPORT_ITEM pItem)
{
	memcpy (&m_ReportItem, pItem, sizeof(REPORT_ITEM));
}

void CDlgReport::OnBnClickedBtnInsertMemo()
{
	CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT_MEMO);
	pEdit->UpdateData(TRUE);

	if (pEdit)
	{
		wsprintf(m_PrintInfo.tzMemo1, _T(""));
		wsprintf(m_PrintInfo.tzMemo2, _T(""));
		wsprintf(m_PrintInfo.tzMemo3, _T(""));
		wsprintf(m_PrintInfo.tzMemo4, _T(""));
		wsprintf(m_PrintInfo.tzMemo5, _T(""));

		int nLineCnt = pEdit->GetLineCount();
		int nLineLen, nFindPos=0;
		TCHAR* pNull = NULL;


		if (nLineCnt >= 1)
		{									
			nLineLen = pEdit->LineLength(nFindPos);
			pEdit->GetLine(0, m_PrintInfo.tzMemo1, nLineLen);			
			nFindPos += (nLineLen + 2);

			pNull = (TCHAR*)(m_PrintInfo.tzMemo1 + nLineLen);
			wsprintf(pNull, _T(""));
		}

		if (nLineCnt >= 2)
		{	
			nLineLen = pEdit->LineLength(nFindPos);
			pEdit->GetLine(1, m_PrintInfo.tzMemo2, nLineLen);
			nFindPos += (nLineLen + 2);

			pNull = (TCHAR*)(m_PrintInfo.tzMemo2 + nLineLen);
			wsprintf(pNull, _T(""));
		}

		if (nLineCnt >= 3)
		{	
			nLineLen = pEdit->LineLength(nFindPos);
			pEdit->GetLine(2, m_PrintInfo.tzMemo3, nLineLen);
			nFindPos += (nLineLen + 2);

			pNull = (TCHAR*)(m_PrintInfo.tzMemo3 + nLineLen);
			wsprintf(pNull, _T(""));
		}

		if (nLineCnt >= 4)
		{		
			nLineLen = pEdit->LineLength(nFindPos);
			pEdit->GetLine(3, m_PrintInfo.tzMemo4, nLineLen);
			nFindPos += (nLineLen + 2);

			pNull = (TCHAR*)(m_PrintInfo.tzMemo4 + nLineLen);
			wsprintf(pNull, _T(""));
		}

		if (nLineCnt >= 5)
		{			
			nLineLen = pEdit->LineLength(nFindPos);
			pEdit->GetLine(4, m_PrintInfo.tzMemo5, nLineLen);
			nFindPos += (nLineLen + 2);

			pNull = (TCHAR*)(m_PrintInfo.tzMemo5 + nLineLen);
			wsprintf(pNull, _T(""));
		}

		Invalidate(FALSE);
	}
}

void CDlgReport::OnBnClickedBtnPrint()
{
	CReportPreview  preview;
	preview.PrintOut(&m_ReportItem, &m_PrintInfo, m_pViewerSetup);
}

void CDlgReport::OnBnClickedCancel()
{
	CDialog::OnCancel();
}

//////////////////////////////////////////////////////////////////////////
void CDlgReport::LoadText()
{
	this->SetWindowText(
		theApp.GetUIString(APPNAME_PRINT, KEYNAME_PRINT_TITLE, _T("Report")));

	GetDlgItem(IDC_STATIC_REPORT)->SetWindowText(
		theApp.GetUIString(APPNAME_PRINT, KEYNAME_PRINT_MEMO, _T("Memo")));
	GetDlgItem(IDC_BTN_INSERT_MEMO)->SetWindowText(
		theApp.GetUIString(APPNAME_PRINT, KEYNAME_PRINT_ADD_MEMO, _T("Insert Memo")));
	GetDlgItem(IDC_BTN_PRINT)->SetWindowText(
		theApp.GetUIString(APPNAME_PRINT, KEYNAME_PRINT_PRINT, _T("Print")));
	GetDlgItem(IDC_CANCEL)->SetWindowText(
		theApp.GetUIString(APPNAME_COMMON, KEYNAME_COMMON_CANCEL, KEYNAME_COMMON_CANCEL));


	m_sarrText.RemoveAll();
	m_sarrText.Add(
		theApp.GetUIString(APPNAME_PRINT, KEYNAME_PRINT_OUT_TITLE, _T("The Occurrence Report")));
	m_sarrText.Add(
		theApp.GetUIString(APPNAME_PRINT, KEYNAME_PRINT_OUT_DATE, _T("Issued Date")));
	m_sarrText.Add(
		theApp.GetUIString(APPNAME_PRINT, KEYNAME_PRINT_OUT_PLACE, _T("Issued Location (GPS)")));
	m_sarrText.Add(
		theApp.GetUIString(APPNAME_PRINT, KEYNAME_PRINT_OUT_GSENSOR, _T("G Sensor")));
	m_sarrText.Add(
		theApp.GetUIString(APPNAME_PRINT, KEYNAME_PRINT_OUT_MEMO, _T("Memo")));
}